/*
 * SelectVariables.java
 *
 * Created as submitButton class                                     on October 12, 2004, 10:09 PM
 * Begin overhaul                                                    on August 15, 2006
 * Functionality of selectIndexes class extracted from submit button on August 18, 2006, 1:19 AM
 *
 * Created on August 1, 2007, 2:02 AM; former selectIndexes.
 * 30, Aug 2007 Moved from package portfolio.stock.submit to portfolio.stock
 * 26, May 2008 In order to prevent more than one stock to be displaied, prevents the creation of other instance of stockMenu.
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package portfolio.stock;


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EtchedBorder;
import portfolio.configMessage.configMessageListener;
import portfolio.dbHost.project.attributes.InputAttribs;

import portfolio.mainScreen;
import misellanea.MiscellaneaDB;
import portfolio.dbHost.dataBases.dbTable.Table;
import portfolio.dbHost.project.Project;

/**
 *
 */
public class SelectVariables implements ActionListener
{
    private mainScreen  fatherFrame;
    private Vector      selectVariablesListeners = new Vector ();
    private JPanel      displayPanel  = null;
    private JPanel      buttonArea;
    private String      sStockName;

    private Vector      vSelectList  = null;

    //ACA 2008
    private JPanel      jBlocksHolder = null;
    private JButton     jSelectVariablesButton = null;
    int iVariablesBlocks = 0;
    JPanel [] JVariablesBlocks = null;

    /**
     *
     * @param evt 
     */
    public void actionPerformed(ActionEvent evt) {
        String sCommand = evt.getActionCommand();

        if (sCommand.equals("SelectVariablesCancel")){  // <<<<< Cancel button cjecked
            for (int iIdx = 0; iIdx < iVariablesBlocks; iIdx++) {
                JVariablesBlocks[iIdx].removeAll();
            }
            jBlocksHolder.removeAll();
            displayPanel.removeAll();

            fatherFrame.displayMessage("SelectVariables.actionPerformed 92", "Cancel selection of variables for: " + sStockName);
            sStockName = null;
            displayPanel.repaint();

            JPanel jSVMPanel = fatherFrame.getSVMParamsPanel();
            jSVMPanel.removeAll();
            jSVMPanel.repaint();
        } else   if (!sCommand.equals ("OK")) {             // <<<<< CheckBoxes
            JCheckBox cb = (JCheckBox)evt.getSource();
                // Determine status
            boolean isSel = cb.isSelected();
            if (isSel) {
                vSelectList.add (evt.getActionCommand ());
            } else {
                vSelectList.remove (evt.getActionCommand ());
            }
        } 
    }    
    /**
     *
     * @param fatherFrameParam 
     * @param sStockNameParam 
     */
    public SelectVariables (mainScreen fatherFrameParam, String sStockNameParam, Vector vSelectListParam)
    {
        fatherFrame   = fatherFrameParam;
        sStockName    = sStockNameParam;            
        displayPanel  = fatherFrameParam.getSelectVariablesPanel();
        
        if (vSelectListParam == null) {
            vSelectList  = new Vector ();
        } else {
            vSelectList = vSelectListParam;
        }

        displayPanel.setLayout (new BoxLayout (displayPanel, BoxLayout.Y_AXIS));
        
        buttonArea = new JPanel ();
        buttonArea.setLayout (new FlowLayout (FlowLayout.RIGHT));
        displayPanel.add (buttonArea, BorderLayout.SOUTH);

        Connection connection = fatherFrame.getDBLoggedHost().getConnection();
        Project project = fatherFrame.getDBLoggedHost().getProject();

        String [] sExcludeTbls = project.getExcludeTables().get();            
        String [] sIncludeTbls = MiscellaneaDB.getIncludeTables(sStockName, sExcludeTbls);

        InputAttribs tmpAttribs = project.getInputAttribs();
//////////////////////////////////////////////////
        String [] sInputAttribs = tmpAttribs.get();
/////////////////////////////////////////////////        
        String sDBName = sInputAttribs [InputAttribs.INPUTATTRIBS_INPUTDB];//tmpAttribs.getInputDB();
        String [] sExcludeCols = project.getExcludeColumns().get();

        jBlocksHolder  = new JPanel ();
        jBlocksHolder.setLayout (new BoxLayout (jBlocksHolder, BoxLayout.Y_AXIS));
        iVariablesBlocks = sIncludeTbls.length;
        JVariablesBlocks     = new JPanel [iVariablesBlocks];

        try {
            for (int i = 0; i < iVariablesBlocks; i++) {
                Vector vTempDescribe = Table.describe (connection, sDBName, sIncludeTbls [i], sExcludeCols);
                Rectangle displayRect = displayPanel.getBounds();
                String sBlockName = null;
                if (i == 0) {
                    sBlockName = "Stock variable: " + sIncludeTbls [i];
                } else {
                    sBlockName = "Variables block: " + sIncludeTbls [i];
                }
                JVariablesBlocks [i] = createPanelArea (sBlockName, displayRect, vTempDescribe, sIncludeTbls [i]);
                JVariablesBlocks [i].setVisible(true);
                jBlocksHolder.add (JVariablesBlocks [i]);
            }

            jSelectVariablesButton = new JButton ("Select Variables");
            buttonArea.add (jSelectVariablesButton);
            //SelectButton analizeButton = new SelectButton (fatherFrame, this, sStockName, jAnalizeButton);

            JButton cancelButton = new JButton ("Cancel");
            buttonArea.add (cancelButton);
            cancelButton.addActionListener(this);
            cancelButton.setActionCommand  ("SelectVariablesCancel");

            JScrollPane jBlocksScroll = new JScrollPane (jBlocksHolder);
            displayPanel.add(jBlocksScroll);
            displayPanel.setVisible(true);
            jBlocksHolder.setVisible(true);
            displayPanel.repaint();
            jBlocksHolder.repaint ();
       } catch (Exception ex) {
           fatherFrame.displayMessage("SelectVariables.selectVariables 215", ex.toString());
       }                    
    } /* End Constructor selectIndexes */
    /**
     *
     */
    private JPanel createPanelArea (String sBorderName, Rectangle panelRect, Vector itemVector, String sItemVectorPrefix)
    {
        if (sBorderName == null)
            return null;
        if (panelRect == null)
            return null;
        if (itemVector == null)
            return null;
        if (sItemVectorPrefix == null)
            return null;

        Vector vCheckBoxes = new Vector ();
        int iStringMax = 0;

        for (int i = 0; i < itemVector.size (); i++) {
            String sBoxName = itemVector.get (i).toString ();
            
            String sBoxAcctionCommand = sItemVectorPrefix + "." + sBoxName;
            JCheckBox chIndex = null;
            if (vSelectList != null) {
                if (vSelectList.contains(sBoxAcctionCommand)) {
                    chIndex = new JCheckBox (sBoxName, true);
                } else {
                    chIndex = new JCheckBox (sBoxName);                    
                }
            }
            chIndex.addActionListener(this);
            chIndex.setActionCommand (sBoxAcctionCommand);

            vCheckBoxes.add(chIndex);

            Font fntBoxName = chIndex.getFont();
            FontMetrics fntMtrcsBox = chIndex.getFontMetrics(fntBoxName);
            int iStringWidth = fntMtrcsBox.stringWidth(sBoxName);
            if (iStringWidth > iStringMax)
                iStringMax = iStringWidth;
        }

        int iWidth = panelRect.width;
        int iCols = iWidth / (iStringMax +25);
        int iRows = itemVector.size() / iCols;
        iRows = ((itemVector.size() % iCols) != 0)? iRows +1 : iRows +0;

        JPanel panelArea = new JPanel ();
        panelArea.setLayout (new GridLayout (iRows, iCols));
        panelArea.setBorder (BorderFactory.createTitledBorder (new EtchedBorder (Color.white, Color.gray), sBorderName));

        for (int i = 0; i < itemVector.size(); i++) {
            JCheckBox tmpCheckBox = (JCheckBox) vCheckBoxes.get(i);
            panelArea.add(tmpCheckBox);
        }

        return panelArea;
    } /* End method createPanelArea */
    /**
     *
     * @return 
     */
    public String getStockName ()
    {
        return sStockName;
    }
    /**
     *
     * @param l 
     */
    public synchronized void removeConfigMessageListener (configMessageListener l)
    {
        selectVariablesListeners.removeElement (l);
    }
    /**
     * 
     */
    public Vector getSelectVariablesList ()
    {
        return vSelectList;
    }
} /* End class selectIndexes */
